package com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.repository.impl;


import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.entity.PersonnelManagement;
import com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.repository.PersonnelManagementRepository;

import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/01/10
 * @Description: TODO
 */
@Repository
public class PersonnelManagementRepositoryImpl implements PersonnelManagementRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private static final String TABLE_NAME = "xczh_personnel_info_management"; // 表名
    private static final String TABLE_ID = "xpim_id"; // 表id字段名

    @Override
    public void save(PersonnelManagement param) {
        String sql = "insert into " + TABLE_NAME +
                "(xpim_id,xsem_id,person_name," +
                "gender,tel,category,comment_info,company,role,unit,uuid," +
                "del_flag,create_user,create_time,province_id,city_id,county_id)" +
                " values(" +
                ":xpimId," +
                ":xsemId," +
                ":personName," +
                ":gender," +
                ":tel," +
                ":category," +
                ":commentInfo," +
                ":company," +
                ":role," +
                ":unit," +
                ":uuid," +
                ":delFlag,:createUser,:createTime,:provinceId,:cityId,:countyId)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(param);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void updateById(PersonnelManagement param) {
        String sql = "update " + TABLE_NAME +
                " set xsem_id=:xsemId,person_name = :personName," +
                "gender = :gender,tel = :tel,category =:category,comment_info=:commentInfo,company=:company,role=:role,unit=:unit," +
                "update_user =:updateUser,update_time =:updateTime where del_flag = '0' and " + TABLE_ID + " = '"+param.getXpimId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(param);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void deleteById(List<String> ids) {
        String sql = "update " + TABLE_NAME + " set del_flag = :delFlag  where " + TABLE_ID + " in (:ids)";
        Map idMap = new HashMap();
        idMap.put("ids", ids);
        idMap.put("delFlag",YNEnum.Y.toString());
        namedParameterJdbcTemplate.update(sql,idMap);
    }


    /**
     * 批量入库
     * @param list
     */
    @Override
    public void batchSave(List<PersonnelManagement> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "INSERT INTO "+TABLE_NAME+" (xpim_id,xsem_id,person_name,gender,tel,category,comment_info," +
                "unit,province_id,city_id,county_id,company,role,del_flag,create_user,create_time,uuid)" +
                "values (:xpimId,:xsemId,:personName,:gender,:tel,:category,:commentInfo," +
                ":unit,:provinceId,:cityId,:countyId,:company,:role,:delFlag,:createUser,:createTime,:uuid)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public Map<String, Object> getPage(PersonnelManagement queParams, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from "+TABLE_NAME);
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        //人员名称
        if (!PlatformObjectUtils.isEmpty(queParams.getPersonName())) {
            whereSql.append(" and person_name like '%" + queParams.getPersonName() + "%'");
        }
        //地震编号
        if (!PlatformObjectUtils.isEmpty(queParams.getXsemId())) {
            whereSql.append(" and xsem_id = '" + queParams.getXsemId() + "'");
        }
        //现场人员类别
        if (!PlatformObjectUtils.isEmpty(queParams.getCategory())) {
            whereSql.append(" and category = '" + queParams.getCategory() + "'");
        }
        //手机号码
        if (!PlatformObjectUtils.isEmpty(queParams.getTel())) {
            whereSql.append(" and tel like '%" + queParams.getTel() + "%'");
        }

        sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(PersonnelManagement.class)).size();
        List<PersonnelManagement> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(PersonnelManagement.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public PersonnelManagement findById(String param) {
        String sql = "select * from " + TABLE_NAME + " where " + TABLE_ID + " = '" + param + "'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(PersonnelManagement.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public void updateXsemId(String xsemId, String convertedXsemId) {
        String sql = "update " + TABLE_NAME + " set xsem_id = '"+xsemId+"'  where xsem_id = '"+convertedXsemId+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public PersonnelManagement findByXsemIdAndTel(String tel, String xsemId) {
        String sql = "select * from " + TABLE_NAME + " where tel='"+tel+"' and xsem_id='"+xsemId+"' and del_flag = '0'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(PersonnelManagement.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public Integer countByXsemId(String xsemId) {
        String sql ="select count(*) from xczh_personnel_info_management where del_flag='0' and xsem_id='"+xsemId+"'";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }

    @Override
    public PersonnelManagement findByXsemIdAndTelAndName(String tel, String name, String xsemId) {
        String sql = "select * from " + TABLE_NAME + " where tel='"+tel+"' and xsem_id='"+xsemId+"' and del_flag = '0' and person_name='"+name+"'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(PersonnelManagement.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }


}
